﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FirmaTransportowa
{
    public partial class AdminAddNewWorker : Form
    {

       
        
       SqlConnection dbConnection;

        public AdminAddNewWorker()
        {
            SqlConnector connection = new SqlConnector();
            dbConnection = connection.getInstance();
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void addButton_Click(object sender, EventArgs e)
        {
            bool fieldsFilledOut = true;
            
            if (nameBox.Text.Trim() == String.Empty)
            {
                nameError.Text = "Podaj imię";
                nameError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }

            if (surnameBox.Text.Trim() == String.Empty)
            {
                surnameError.Text = "Podaj nazwisko";
                surnameError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }

            /*if (birthBox.Text.Trim() == String.Empty)
            {
                birthError.Text = "Podaj datę urodzenia";
                birthError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }
            */
            if (peselBox.Text.Trim() == String.Empty)
            {
                peselError.Text = "Podaj PESEL";
                peselError.ForeColor = Color.DarkRed;
                fieldsFilledOut = false;
            }

            if ( (fieldsFilledOut) && (!checkIfRecordExists(peselBox.Text.Trim().ToString(), "Workers") ) )
            {
                dbConnection.Open();
                SqlCommand cmd = new SqlCommand("INSERT INTO Workers (name, surname, birthdate, pesel, occupation_ID) VALUES (@name, @surname, @birthdate, @pesel, @occupation_ID)");
                cmd.Connection = dbConnection;
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@name", nameBox.Text.Trim().ToString());
                cmd.Parameters.AddWithValue("@surname", surnameBox.Text.Trim().ToString());
                cmd.Parameters.AddWithValue("@birthdate", dateBox.Value.ToShortDateString());
                cmd.Parameters.AddWithValue("@pesel", peselBox.Text.Trim().ToString());
                string spoko = occupationBox.SelectedItem.ToString();
                int occupationID = 1;
                switch (spoko)
                {
                    case "admin":
                        occupationID = 1;
                        break;
                    case "uzytkownik":
                        occupationID = 2;
                        break;
                    case "dyspozytor":
                        occupationID = 3;
                        break;
                    case "kasjer":
                        occupationID = 4;
                        break;
                    case "statystyk":
                        occupationID = 5;
                        break;
                }
                cmd.Parameters.AddWithValue("@occupation_ID", occupationID);
                cmd.ExecuteNonQuery();
                dbConnection.Close();
            }

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void AdminAddNewUser_Shown(object sender, EventArgs e)
        {
            try
            {
                dbConnection.Open();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

        }

        private void AdminAddNewWorker_Load(object sender, EventArgs e)
        {

        }

        public static bool checkIfRecordExists(string checkedText, string desiredTable)
        {
            bool result = false;

            SqlConnection dbConnection2 = new SqlConnection("server=Grxai-PC\\witam;" +
                                        "Trusted_Connection=yes;" +
                                        "database=transport.dbo;" +
                                        "connection timeout=30;" +
                                        "Initial Catalog=Transport");

            dbConnection2.Open();

            try
            {
                string sql = "SELECT * FROM " + desiredTable + " WHERE pesel='" + checkedText + "'";

                SqlCommand cmd = new SqlCommand(sql, dbConnection2);
                SqlDataReader reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    result = true;
                    MessageBox.Show("Podany numer PESEL juz istnieje w bazie!");
                }
                reader.Close();
            }
            finally
            {
                dbConnection2.Close();
            }

            return result;
        }

    }
}
